﻿using System;
using System.Collections.Generic;
using System.Linq;
using App_Code.Helpers;
using System.Text;
using System.Text.RegularExpressions;
using PluginDB;

namespace App_Code.SiteParsers
{

    /// <summary>
    /// Summary description for SdarotOrg
    /// </summary>
    public class SdarotOrg : BaseScanner
    {
        private const string VIDEO_TYPE_NAME = "סדרות";
        private const string CATEGORIES_URL = "http://www.sdarot.org/categories";
        private const string BASE_URL = "http://www.sdarot.org";
        protected const string SEARCH_URL = "http://www.sdarot.org/search?search_query={0}&search_type=videos";
        
        private class SeriesItem
        {
            public string Name { get; set; }
            public string ImageUrl { get; set; }
            public string PageUrl { get; set; }
        }

        public SdarotOrg()
        {
            this.Name = "Sdarot.org";
        }

        #region Private Methods
        private IEnumerable<SeriesItem> LoadCategories()
        {
            var page = Utils.GetPage(CATEGORIES_URL, Encoding.UTF8);

            const string categoriesLinkRegEx = "<div class=\"category_box\">.*?<a href=\"(.+?)\"><img src=\"(.+?)\".*?<div.*?>(.+?)</div>";
            var matches = Regex.Matches(page, categoriesLinkRegEx, RegexOptions.Singleline);

            return (from Match match in matches
                    select new SeriesItem
                               {
                                   Name = this.CleanString(match.Groups[3].Value), 
                                   ImageUrl = BASE_URL + match.Groups[2].Value, 
                                   PageUrl = BASE_URL + match.Groups[1].Value
                               }).ToList();
        }       

        private void LoadMovies(SeriesItem seriesItem)
        {
            try
            {
                var page = Utils.GetPage(seriesItem.PageUrl, Encoding.UTF8);

                const string movieSectionRegEx = "<div class=\"video_box\">.*? <a href=\"(.+?)\"><img src=\"(.+?)\" title=\"(.+?)\"";
                var matches = Regex.Matches(page, movieSectionRegEx, RegexOptions.Singleline);

                foreach (Match match in matches)
                {
                    var moviePageLink = BASE_URL + match.Groups[1].Value;
                    var imageLink = BASE_URL + match.Groups[2].Value;
                    var movieTitle = match.Groups[3].Value;

                    if (IsVideoExists(moviePageLink))
                        continue;

                    var video = new PluginVideoInfo
                    {
                        VideoName = movieTitle,
                        PluginUrl = moviePageLink,
                        SiteName = this.Name,
                        IsTVShow = true,
                        ImageUrl = imageLink,
                        IsHebSub = true
                    };

                    video.Categories.Add(seriesItem.Name);

                    video.Links.Add(new PluginVideoInfo.Link
                    {
                        LinkURL = moviePageLink,
                        SourceName = this.Name
                    });

                    AddVideoToDB(video);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        #endregion

        #region Implementation of ISiteScanner
        public override void ScanForMovies(int pagesToScan)
        {
            
        }

        public override void ScanForSeries(int pagesToScan)
        {
            var categories = this.LoadCategories();
            foreach (var category in categories)
            {
                this.LoadMovies(category);
            }
                
        }
        #endregion
    }

}